//
//  SWStatusOriginalView.swift
//  MYSwiftWeiBo
//
//  Created by shingwai chan on 16/3/4.
//  Copyright © 2016年 ChanShingWai. All rights reserved.
//

import UIKit
import SnapKit

let SWStatusCellMargin: CGFloat = 8.0

class SWStatusOriginalView: UIView {
    
    //记录当前view的底部约束
    var statusOriginalViewBottom: Constraint?
    
    //当赋值status
    var status:SWStatus? {
        didSet {
            
            //头像
            if let profile_image_url = status?.user?.profile_image_url {
                
                let imgURL = NSURL(string: profile_image_url)
                self.iconView.sd_setImageWithURL(imgURL, placeholderImage: UIImage(named: "avatar"))
            }
            //用户名
            namelabel.text = status?.user?.screen_name
            //时间
            timeLabel.text  = NSDate.sinaDateStringToDate(status?.created_at ?? " ").sinaDateDescription()
            //来源
            sourceLabel.text = status?.source
            //会员等级
            memberView.image = status?.user?.mbrankImage
            //认证
            verifiedTypeImage.image = status?.user?.verified_typeImage
            //微博内容
            countentLabel.text = status?.text
            
            //微博图片
            //先移除上次的约束
            statusOriginalViewBottom?.uninstall()
            if let picture = status?.pic_urls where picture.count > 0 {
                //有图片
                pictureView.hidden = false
                //模型赋值给pictureView
                pictureView.status = status
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                   self.statusOriginalViewBottom =  make.bottom.equalTo(pictureView).offset(SWStatusCellMargin).constraint
                })
            }else{
                //没有配图
                pictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                   self.statusOriginalViewBottom = make.bottom.equalTo(countentLabel).offset(SWStatusCellMargin).constraint
                })
            
            }
            
        }
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        prepareUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func prepareUI() {
    
        addSubview(iconView)
        addSubview(namelabel)
        addSubview(memberView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedTypeImage)
        addSubview(countentLabel)
        addSubview(pictureView)
        //约束
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(SWStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        //用户名
        namelabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(SWStatusCellMargin)
        }
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(SWStatusCellMargin)
            make.bottom.equalTo(iconView)
        }
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(SWStatusCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        //会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(namelabel)
            make.leading.equalTo(namelabel.snp_trailing).offset(SWStatusCellMargin)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        //认证图标
        verifiedTypeImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        //微博内容
        countentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(SWStatusCellMargin)
            make.leading.equalTo(self).offset(SWStatusCellMargin)
            make.trailing.equalTo(self).offset(-SWStatusCellMargin)
        }
        
        //微博配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(SWStatusCellMargin)
            make.top.equalTo(countentLabel.snp_bottom).offset(SWStatusCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        //设置当前view
        self.snp_makeConstraints { (make) -> Void in
          //记录当前的view的底部约束
            self.statusOriginalViewBottom = make.bottom.equalTo(pictureView).offset(SWStatusCellMargin).constraint
        }
        
    }
    
    
    
    //MARK: 懒加载
    //头像
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    //名字
    lazy var namelabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), font: 14)
    //会员等级
    lazy var memberView: UIImageView = UIImageView(image: nil)
    //时间
    lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), font: 11)
    //来源
    lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), font: 11)
    //认证类型
    lazy var verifiedTypeImage: UIImageView = UIImageView(image: nil)
    //内容
    lazy var countentLabel: UILabel = {
        
        let contLabel = UILabel(textColor: UIColor.darkGrayColor(), font: 15)
        contLabel.numberOfLines = 0
        return contLabel
    }()
    //微博配图
    lazy var pictureView: SWStatusPictureView = SWStatusPictureView()
}
